package com.cly.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cly.base.contants.CacheKeysConstants;
import com.cly.entity.EducationExperience;
import com.cly.mapper.EducationExperienceMapper;
import com.cly.result.JsonRequest;
import com.cly.service.IEducationExperienceService;
import com.cly.vo.EducationExperienceVo;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 教育经历表 服务实现类
 *
 * @author by 春风能解释
 * <p>
 * 2024/11/9
 */
@Service
@RequiredArgsConstructor
public class EducationExperienceServiceImpl extends ServiceImpl<EducationExperienceMapper, EducationExperience> implements IEducationExperienceService {
    /**
     * 查看教育经历
     *
     * @param resumeId 简历id
     * @return JSON
     */
    @Override
    @Cacheable(value = CacheKeysConstants.RecruitKeys.EDUCATION_EXPERIENCE_ARRAY, key = "#resumeId")
    public JsonRequest<List<EducationExperienceVo>> lookOneEducationExperience(Long resumeId) {
        return JsonRequest.success(baseMapper.lookOneEducationExperience(resumeId));
    }

    /**
     * 新增教育经历并关联简历
     *
     * @param educationExperience 教育经历
     * @param resumeId            简历id
     * @return JSON
     */
    @Override
    @CacheEvict(value = CacheKeysConstants.RecruitKeys.EDUCATION_EXPERIENCE_ARRAY, key = "#resumeId")
    public JsonRequest<Boolean> addOneEducationExperience(List<EducationExperience> educationExperience, Long resumeId) {
        return JsonRequest.success(saveOrUpdateBatch(educationExperience
                .stream()
                .map(v -> v.setResumeId(resumeId))
                .toList()));
    }

    /**
     * 删除教育经历
     *
     * @param resumeId 项目经历id
     * @return JSON
     */
    @Override
    @CacheEvict(value = CacheKeysConstants.RecruitKeys.EDUCATION_EXPERIENCE_ARRAY, allEntries = true)
    public JsonRequest<Boolean> deleteEducationExperience(Long resumeId) {
        return JsonRequest.success(remove(Wrappers.lambdaQuery(EducationExperience.class)
                .eq(EducationExperience::getResumeId, resumeId)));
    }
}




